package com.yh.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author by 张晨光
 * @date 2024/1/18 9:04
 */
@Controller
public class ModelSaveController {
    /**
     * 1.原生方式；
     *      index.jsp  ->ModelSaveController        存数据;
     *                         -> list.jsp
     * @return
     */
    @RequestMapping("requestAPI")
    public String requestAPI(HttpServletRequest request, HttpServletResponse response){
        //作业：不让写下面的范式，如何来获取页面index.jsp里面？username=tom的数据
        String username = request.getParameter("username");
        request.setAttribute("username",username);

        //request.setAttribute("username","tom");
        //...其他的类似
        return "forward:/list.jsp";
    }

    @RequestMapping("modelandview")
    public ModelAndView modelandview(String username,String password,ModelAndView mv){
        //mv.addObject("username","pony.ma");     //马化腾

        mv.addObject("username",username);
        mv.addObject("password",password);
        mv.setViewName("forward:/list.jsp");
        return mv;
    }

    @RequestMapping("model")
    public String modeltest(Model model){
        //通过mode参数来设置数据;
        model.addAttribute("username","jack.ma"); //马云
        return "forward:/list.jsp";
    }
}
